<template>
  <div class="sights-booking">
    <!-- 搜索 -->
    <div class="form admin-common-bg">
      <el-form
        :inline="true"
        :model="formData"
        :rules="rules"
        ref="formData"
        style="text-align: left"
      >
        <el-form-item label="预约发起人" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入发起人姓名"
            size="mini"
            :clearable="true"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="预约状态" prop="status">
          <el-select
            v-model="formData.status"
            size="mini"
            :clearable="true"
            style="width: 100px"
          >
            <el-option
              v-for="item in bookingStatusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="search">
            <i class="el-icon-search"></i>查询
          </el-button>
          <el-button size="mini" @click="reset">
            <i class="el-icon-refresh"></i>重置
          </el-button>
          <el-button
            type="success"
            size="mini"
            :loading="exportLoading"
            @click="exportExcel"
          >
            <i class="el-icon-document"></i>导出Excel
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据列表 -->
    <div class="content admin-common-bg" v-loading="loading">
      <div v-if="dataList.length > 0">
        <el-collapse accordion>
          <el-collapse-item
            v-for="item in dataList"
            :key="item.id"
            :disabled="item.status === 0"
          >
            <template slot="title">
              <div class="header">
                <div class="header-item">
                  <span>预约发起人：{{ item.name }}</span>
                </div>
                <div class="header-item">
                  <span>预约时间：{{ formatDate(item.bookingTime) }}</span>
                </div>
                <div class="header-item">
                  <span>预约人数：{{ item.number }}</span>
                </div>
                <div class="header-item">
                  <span>景点名：{{ item.sightsName }}</span>
                </div>
                <div class="header-item">
                  <span
                    >总费用（元）：{{
                      item.price === 0 ? "免费" : item.price
                    }}</span
                  >
                </div>
                <div class="header-item">
                  <span>
                    当前状态：
                    <el-tag
                      :type="
                        item.status === -1
                          ? 'danger'
                          : item.status === 0
                          ? 'info'
                          : item.status === 1
                          ? 'success'
                          : 'primary'
                      "
                      >{{
                        item.status === -1
                          ? "已过期"
                          : item.status === 0
                          ? "已取消"
                          : item.status === 1
                          ? "已预约"
                          : "已使用"
                      }}</el-tag
                    >
                  </span>
                </div>
              </div>
            </template>
            <div class="title">
              <h3>人员信息</h3>
              <el-link type="info" @click="mark = true">查看</el-link>
            </div>
            <div class="booking-person" v-for="info in item.bookingPersonList">
              <div class="item">姓名：{{ info.name }}</div>
              <div class="item">手机号：{{ info.phone }}</div>
              <div class="item">身份证号：{{ info.idCard }}</div>
            </div>
            <!-- 遮罩，展示封面 -->
            <div class="mark" @click="mark = false" v-show="mark">
              <div class="cover">
                <img :src="item.cover" alt="" />
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
        <!-- 分页 -->
        <el-pagination
          v-show="dataList.length > 0"
          background
          layout="prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize"
          @current-change="handleCurrentChange"
          :page-count="7"
          style="text-align: right; margin-top: 24px"
        >
        </el-pagination>
      </div>
      <el-empty
        v-else="dataList.length === 0"
        description="暂无数据"
      ></el-empty>
    </div>
  </div>
</template>

<script>
import {
  exportSightsBookingList,
  getSightsBookingList,
} from "@/api/admin/booking";
export default {
  name: "SightsBooking",
  data() {
    return {
      dataList: [],
      bookingStatusList: [
        { label: "已预约", value: 1 },
        { label: "已使用", value: 2 },
        { label: "已取消", value: 0 },
        { label: "已过期", value: -1 },
      ],
      formData: {
        name: "",
        status: "",
      },
      rules: {
        name: [{ validator: this.customValidator, trigger: "blur" }],
        status: [{ validator: this.customValidator, trigger: "blur" }],
      },
      page: 1,
      pageSize: 10,
      currentPage: 1,
      total: 0,
      loading: false,
      // 遮罩
      mark: false,
      // 导出Excel加载
      exportLoading: false,
    };
  },
  created() {
    this.getSightsBookingList();
  },
  methods: {
    // 获取景点预订列表
    async getSightsBookingList() {
      try {
        this.loading = true;
        const res = await getSightsBookingList({
          page: this.page,
          pageSize: this.pageSize,
          ...this.formData,
        });
        this.dataList = res.data.records;
        this.total = res.data.total;
      } catch {
        this.$message.error("获取列表失败");
      } finally {
        this.loading = false;
      }
    },
    // 自定义校验规则
    customValidator(rule, value, callback) {
      const { name, status } = this.formData;
      if (!name && status !== 0 && !status) {
        callback(new Error("请填写信息"));
      } else {
        callback();
      }
    },
    // 搜索
    search() {
      this.$refs.formData.validate((v) => {
        if (v) {
          this.page = 1;
          this.currentPage = 1;
          this.getSightsBookingList();
        }
      });
    },
    // 重置
    reset() {
      this.page = 1;
      this.currentPage = 1;
      this.$refs.formData.resetFields();
      this.getSightsBookingList();
    },
    // 分页
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getSightsBookingList();
    },
    // 时间格式判断
    padZero(num) {
      return num < 10 ? "0" + num : num;
    },
    // 时间格式化
    formatDate(timeArray) {
      if (!timeArray) return "暂无时间";
      let year = timeArray[0];
      let month = this.padZero(timeArray[1]);
      let day = this.padZero(timeArray[2]);
      return `${year}-${month}-${day}`;
    },
    // 导出Excel
    async exportExcel() {
      try {
        this.exportLoading = true;
        const res = await exportSightsBookingList();
        // 根据返回的路径创建a标签并下载
        const a = document.createElement("a");
        a.href = res.data;
        a.download = "景点预订列表.xlsx";
        a.click();
      } finally {
        this.exportLoading = false;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.sights-booking {
  position: relative;
  width: 100%;
  height: 100%;
  margin-bottom: 16px;
  .form {
    padding-left: 20px;
    height: 66px;
    i {
      margin-right: 4px;
    }
  }
  .content {
    width: 100%;
    min-height: 294px;
    margin-top: 14px;
    padding: 0 20px;
    padding-bottom: 10px;
    text-align: left;
    .header {
      display: flex;
      width: 100%;
      height: 100px;
      margin-top: 50px;
      font-size: 14px;
      .header-item {
        margin-right: 20px;
        &:nth-of-type(1) {
          width: 160px;
          span {
            display: inline-block;
            width: 100%;
            height: 40px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
        &:nth-of-type(2) {
          width: 150px;
        }
        &:nth-of-type(3) {
          width: 110px;
        }
        &:nth-of-type(4) {
          width: 160px;
          span {
            display: inline-block;
            width: 100%;
            height: 40px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
        &:nth-of-type(5) {
          width: 140px;
        }
      }
    }
    .title {
      display: flex;
      justify-content: space-between;
      align-items: flex-end;
      width: 140px;
    }
    .booking-person {
      display: flex;
      margin-left: 20px;
      width: 800px;
      height: 50px;
      line-height: 50px;
      box-shadow: 3px 2px 8px 1px rgba(0, 0, 0, 0.1);
      margin: 12px 20px;
      padding-left: 8px;
      .item {
        margin-right: 10px;
        &:nth-of-type(1) {
          width: 160px;
        }
        &:nth-of-type(2) {
          margin-right: 80px;
        }
      }
    }
  }
  .mark {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    .cover {
      width: 450px;
      height: 340px;
      background-color: pink;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>